<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="申领人" prop="userId">
        <el-select v-model="queryParams.userId" clearable placeholder="请选择申领人">
            <el-option
              v-for="user in this.userOptions"
              :key="user.id"
              :label="user.name"
              :value="user.id"
            />
          </el-select>
      </el-form-item>
      <el-form-item label="救济金类型" prop="benefitType">
          <el-select v-model="form.benefitType" clearable placeholder="请选择救济金类型">
            <el-option
              v-for="dict in dict.type.benefit_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      <el-form-item label="救济金金额" prop="benefitAmount">
        <el-input
          v-model="queryParams.benefitAmount"
          placeholder="请输入救济金金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申领时间" prop="claimDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.claimDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择申领时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['rural:applicationBenefitAnagement:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['rural:applicationBenefitAnagement:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['rural:applicationBenefitAnagement:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['rural:applicationBenefitAnagement:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applicationBenefitAnagementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="申领人" align="center" prop="userId" :formatter="userFormatter" />
      <el-table-column label="救济金类型" align="center" prop="benefitType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.benefit_type" :value="scope.row.benefitType"/>
        </template>
      </el-table-column>
      <el-table-column label="救济金金额" align="center" prop="benefitAmount" />
      <el-table-column label="申领时间" align="center" prop="claimDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.claimDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申领状态" align="center" prop="claimStatus" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.claim_status" :value="scope.row.claimStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="申领原因" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.claimStatus === '1'"
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="confirm(scope.row)"
            v-hasPermi="['rural:ruralBenefitAnagementDonate:edit']"
          >确认</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['rural:applicationBenefitAnagement:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['rural:applicationBenefitAnagement:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改救济金申领对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-form-item label="申领人" prop="userId">
          <el-select v-model="form.userId" clearable style="width:380px" placeholder="请选择申领人">
            <el-option
              v-for="user in this.userOptions"
              :key="user.id"
              :label="user.name"
              :value="user.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="救济金类型" prop="benefitType">
          <el-select v-model="form.benefitType" clearable style="width:380px" placeholder="请选择救济金类型">
            <el-option
              v-for="dict in dict.type.benefit_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="救济金金额" prop="benefitAmount">
          <el-input v-model="form.benefitAmount" placeholder="请输入救济金金额" />
        </el-form-item>
        <el-form-item label="申领时间" prop="claimDate">
          <el-date-picker clearable size="small"
            v-model="form.claimDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择申领时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申领原因" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入申领原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { confirmApplicationBenefitAnagement, listApplicationBenefitAnagement, getApplicationBenefitAnagement, delApplicationBenefitAnagement, addApplicationBenefitAnagement, updateApplicationBenefitAnagement } from "@/api/rural/applicationBenefitAnagement";
import { listRuralUser } from "@/api/rural/ruralUser";

export default {
  name: "ApplicationBenefitAnagement",
  dicts: ['benefit_type','claim_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 救济金申领表格数据
      applicationBenefitAnagementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        benefitType: null,
        benefitAmount: null,
        claimDate: null,
        claimStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
            { required: true, message: '请选择申领人', trigger: 'change' }
        ],
        benefitType: [
            { required: true, message: '请选择救济金类型', trigger: 'change' }
        ],
        benefitAmount: [
            { required: true, message: '请输入救济金金额', trigger: 'blur' }
        ],
        claimDate: [
            { required: true, message: '请选择申领时间', trigger: 'change' }
        ]
      },
      userOptions: []
    };
  },
  created() {
    listRuralUser().then(response => {
      this.userOptions = response.rows
    })
    this.getList();
  },
  methods: {
    confirm(row){
      console.log(row)
      this.$modal.confirm('是否确认资金已到账？').then(()=> {
        row.donateStatus = '2'
        confirmApplicationBenefitAnagement(row).then(response => {
          this.getList();
          this.$modal.msgSuccess(response.message);
        });
      }).catch(() => {});
    },
    userFormatter(row, column){
      for(let i = 0;i < this.userOptions.length;i++){
        if(this.userOptions[i].id === row.userId){
          return this.userOptions[i].name
        }
      }
    },
    /** 查询救济金申领列表 */
    getList() {
      this.loading = true;
      listApplicationBenefitAnagement(this.queryParams).then(response => {
        this.applicationBenefitAnagementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        benefitType: null,
        benefitAmount: null,
        claimDate: null,
        claimStatus: "1",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加救济金申领";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getApplicationBenefitAnagement(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改救济金申领";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateApplicationBenefitAnagement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApplicationBenefitAnagement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除救济金申领编号为"' + ids + '"的数据项？').then(function() {
        return delApplicationBenefitAnagement(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('rural/applicationBenefitAnagement/export', {
        ...this.queryParams
      }, `applicationBenefitAnagement_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
